<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<?php
function check_is_login()
{
	$my_obj=& get_instance();
	$user_name = $my_obj->session->userdata("sys_user_name");
	$md5_encode_str = $my_obj->session->userdata("sys_md5_encode_str");
	$md5_encode_key = $my_obj->config->item('md5_encode_key');
	if( (! $user_name) ||  (! $md5_encode_str) ||  ($md5_encode_str != md5($user_name.$md5_encode_key)))
	{
		echo_msg('<li>登陆超时，或者您还没登陆</li>',site_url(CTL_FOLDER.'login'),'no','_top');
	}
}
	
function create_thumb($pic,$w=0,$h=0)
{
	//如果默认值，则调用配置文件的值
	$ms = get_loaded_extensions();
	if(in_array("gd",$ms))
	{
		$config['image_library'] = 'gd2';
	}
	else
	{
		return false;
	}
	$my_obj=& get_instance();
	if($w==0||$h==0)
	{
		$my_obj->config->load('customer_image_config', FALSE, TRUE);
		$config['width']=$my_obj->config->item("thumb_width");
		$config['height']=$my_obj->config->item("thumb_height");
	}
	else
	{
		$config['width'] = $w;
		$config['height'] = $h;
	}
	$config['create_thumb'] = TRUE;
	$config['maintain_ratio'] = TRUE;
	$my_obj->load->library('image_lib'); 
	if(is_array($pic))
	{
		foreach($pic as $row)
		{
			$config['source_image'] = $row->big_pic_path;
			$my_obj->image_lib->initialize($config); 
			$my_obj->image_lib->resize();
			$my_obj->image_lib->clear();
		}
	}
	else
	{
		$config['source_image'] = $pic;
		$my_obj->image_lib->initialize($config); 
		$my_obj->image_lib->resize();
	}
	return TRUE;
}

function create_watermark($pic)
{
	$ms = get_loaded_extensions();
	if(in_array("gd",$ms))
	{
		$config['image_library'] = 'gd2';
	}
	else
	{
		return false;
	}
	$my_obj=& get_instance();
	$my_obj->config->load('customer_image_config', FALSE, TRUE);
	$config['wm_text'] = $my_obj->config->item("wm_text");
	$config['wm_type'] = $my_obj->config->item("wm_type");
	$config['wm_font_size'] = $my_obj->config->item("wm_font_size");
	$config['wm_font_color'] = str_replace("#","",$my_obj->config->item("wm_font_color"));
	$config['wm_overlay_path'] = $my_obj->config->item("wm_overlay_path");
	$config['wm_vrt_alignment'] = $my_obj->config->item("wm_vrt_alignment");
	$config['wm_hor_alignment'] = $my_obj->config->item("wm_hor_alignment");
	
	$my_obj->load->library('image_lib'); 
	if(is_array($pic))
	{
		foreach($pic as $row)
		{
			$config['source_image'] = $row->big_pic_path;
			$my_obj->image_lib->initialize($config); 
			$my_obj->image_lib->watermark();
			$my_obj->image_lib->clear();
		}
	}
	else
	{
		$config['source_image'] = $pic;
		$my_obj->image_lib->initialize($config); 
		$my_obj->image_lib->watermark();
	}
	return TRUE;
} 

function send_email($to,$sb,$mg)
{
	$my_obj=& get_instance();
	$my_obj->config->load('customer_email_config', FALSE, TRUE);
	$my_obj->config->load('customer_site_config', FALSE, TRUE);
	$config['smtp_host'] = $my_obj->config->item('smtp_host');
	$config['smtp_user'] = $my_obj->config->item('smtp_user');
	$config['smtp_pass'] = $my_obj->config->item('smtp_pass');
	$config['protocol'] = 'smtp';
	$config['charset'] = 'utf-8';
	$config['mailtype'] = 'html';
	$config['wordwrap'] = TRUE;
	
	$my_obj->load->library('email',$config);
	$my_obj->email->from($my_obj->config->item('smtp_email'), $my_obj->config->item('sys_site_name'));
	$my_obj->email->to($to); 
	
	$my_obj->email->subject($sb);
	$my_obj->email->message($mg); 
	if($my_obj->email->send())
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function replace_email_template($document)
{
$document = trim($document);
if (strlen($document) <= 0)
{
  return $document;
}  
$my_obj=& get_instance();
$my_obj->config->load('customer_site_config',FALSE,TRUE);
$search = array ("/\{tpl_site_name\}/i",
                  "/\{tpl_send_time\}/i",
                  "/\{tpl_admin_login_url\}/i", 
				  "/\{tpl_site_url\}/i"
        );

$replace = array (
	$my_obj->config->item('sys_site_name'), 
	date("Y-m-d H:i:s",time()),
	site_url('admin/login'),
	base_url()
);
return @preg_replace ($search, $replace, $document);
}

function get_email_template($id=0)
{
	$my_obj=& get_instance();
	$query=$my_obj->db->get_where('email_template',array('id'=>$id));
	if($query->num_rows()>0)
	{
		return $query->row();
	}
}


function get_cart_list($id)
{
  $my_obj=& get_instance();
  $my_obj->db->select('title,price,pic_path,quantity,(quantity * price) AS subtotal');
  $my_obj->db->where('order_id',$id);
  $query=$my_obj->db->get('order_product');
  if($query->num_rows()>0)
  {
	return $query->result();
  }
  else
  {
  	return array();
  }
}
?>
